package com.bookstore;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

@WebServlet(name = "CookieServlet08", value = "/CookieServlet08")
public class CookieServlet08 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //创建Cookie对象
        request.setCharacterEncoding("UTF-8");
        response.addHeader("Content-type","text/html;charset=utf-8");
        String username = request.getParameter("username");
        System.out.println(username);
        username=URLEncoder.encode(username,"UTF-8");
        Cookie cookie = new Cookie("username",username);
        cookie.setMaxAge(60*60*7);
        response.addCookie(cookie);
        String value1 = cookie.getValue();
        value1 = URLDecoder.decode(value1,"UTF-8");
        System.out.println("username:"+value1);
    }
}
